package ${sourceBasePackage}.service.impl<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}DTO;
import ${sourceBasePackage}.dto<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}SearchDTO;
import ${sourceBasePackage}.model<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName};
import ${sourceBasePackage}.mapper<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Mapper;
import ${sourceBasePackage}.service<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}Service;
import ${sourceBasePackage}.vo<#if sourceIncludeModule == 'Y'>.${moduleName}</#if>.${entityName}PageVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${description}Service实现类
 *
 * @author ${author}
 * @since ${createDate}
 */
@Service("${entityName}Service")
@HussarDs("master")
public class ${entityName}ServiceImpl extends HussarServiceImpl<${entityName}Mapper, ${entityName}> implements ${entityName}Service {

	private static final Logger logger = LoggerFactory.getLogger(${entityName}ServiceImpl.class);

	private static final String RETURN_CODE = "0";

	@Autowired
	private ${entityName}Mapper ${entityName?uncap_first}Mapper;

	@Override
	public ApiResponse<${entityName}PageVO> listByPage(${entityName}SearchDTO ${entityName?uncap_first}SearchDTO) {
		try {
			Page<${entityName}DTO> pager = new Page<>(${entityName?uncap_first}SearchDTO.getCurrent(), ${entityName?uncap_first}SearchDTO.getSize());
			<#list searchFields as po>
				<#if po.listQuery == 'interval' && po.propertyType == 'Date'>
			if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
				${entityName?uncap_first}SearchDTO.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDTO.get${po.propertyName?cap_first}End()));
			}
				</#if>
			</#list>

			${entityName}PageVO ${entityName?uncap_first}PageVO = new ${entityName}PageVO();
			List<${entityName}DTO> list = ${entityName?uncap_first}Mapper.listByPage(pager, ${entityName?uncap_first}SearchDTO);
			${entityName?uncap_first}PageVO.setData(list);
			${entityName?uncap_first}PageVO.setCount(pager.getTotal());
			${entityName?uncap_first}PageVO.setCode(RETURN_CODE);

			return ApiResponse.success(${entityName?uncap_first}PageVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new HussarException("列表查询失败");
		}
	}

	@Override
	public ApiResponse<${entityName}DTO> getById(${pkPropertyType} ${pkPropertyName}) {
		try {
			if(ObjectUtils.isEmpty(${pkPropertyName})) {
				throw new HussarException("参数不正确");
			}
			${entityName}DTO ${entityName?uncap_first}DTO = ${entityName?uncap_first}Mapper.getById(${pkPropertyName});
			if(${entityName?uncap_first}DTO == null) {
				throw new HussarException("数据不存在");
			}

			return ApiResponse.success(${entityName?uncap_first}DTO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new HussarException("表单查询失败");
		}
	}

	@Override
	@HussarTransactional(rollbackFor = Exception.class)
	public ApiResponse<String> add(${entityName}DTO ${entityName?uncap_first}DTO) {
		try {
			${entityName} ${entityName?uncap_first} = BeanUtil.toBean(${entityName?uncap_first}DTO, ${entityName}.class);
			this.saveOrUpdate(${entityName?uncap_first});

			return ApiResponse.success(String.valueOf(${entityName?uncap_first}.getId()), "");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new HussarException("新增失败");
		}

	}

	@Override
	@HussarTransactional(rollbackFor = Exception.class)
	public ApiResponse<String> update(${entityName}DTO ${entityName?uncap_first}DTO) {
		try {
			${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.selectById(${entityName?uncap_first}DTO.get${pkPropertyName?cap_first}());
			if(${entityName?uncap_first} == null) {
				throw new HussarException("数据不存在");
			}
			CopyOptions copyOptions = new CopyOptions();
			copyOptions.setIgnoreNullValue(true);
			BeanUtil.copyProperties(${entityName?uncap_first}DTO, ${entityName?uncap_first}, copyOptions);
			this.updateById(${entityName?uncap_first});

			return ApiResponse.success(String.valueOf(${entityName?uncap_first}.getId()), "");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new HussarException("新增或修改失败");
		}
	}

	@Override
	@HussarTransactional(rollbackFor = Exception.class)
	public ApiResponse<Boolean> delete(${pkPropertyType} ${pkPropertyName}) {
		try {
			${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.selectById(${pkPropertyName});
			if(${entityName?uncap_first} == null) {
            	throw new HussarException("数据不存在");
        	}

			return ApiResponse.success(this.removeById(${pkPropertyName}));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new HussarException("删除失败");
		}
	}

}
